def fib(times):
    n = 0
    a,b = 0,1
    print("start")
    while n<times:
        yield b      # 每次next后都会卡在这句并返回b
        a,b = b,a+b
        n+=1
    print("end")
    return 'done'

g1 = fib(6)
print(next(g1))
print(next(g1))
print(next(g1))
print(next(g1))
print(g1.__next__()) # next(g1) 等价于 g1.__next__()
print('------------------------------')

# for遍历生成器(推荐方式)
g2 = fib(4)
for a in g2:
    print(a)
print('------------------------------')



# 测试send
def fib2(times):
    n = 0
    a,b = 0,1
    print("start")
    while n<times:
        arg1 = yield b  # send进来的参数就是arg1
        print(arg1) 
        a,b = b,a+b
        n+=1
    return 'done'


g3 = fib2(5)
value0 = next(g3)   # 第一次不能是send, 不然报错
value1 = g3.send('Hello')
print(value1)
value2 = g3.send('World')
print(value2)